#pragma once

#include <db_schema/TableDL.hpp>

#include "exports.hpp"
#include "DataReader.hpp"
#include "DataWriter.hpp"




/**
 * The DatabaseProvider is the default provider for any DataProcessor (except some core processors).
 * This provider fetches all replies from a database (sqlite mostly).
 *
 * For performance reasons, this provider pre-fetches several dozens of replies at once, in order to
 * conserve load on the sql server.
 */
class CRAWLER_API DatabaseDataProvider
	: public DataReader
	, public DataWriter
{
public:

	typedef boost::shared_ptr<DatabaseDataProvider>         Ptr;
	typedef boost::shared_ptr<const DatabaseDataProvider>   const_Ptr;
	typedef QList<Reply>                                    Replies;

private:

	db::Connection::Ptr      m_connection;
	db::TableDL::const_Ptr   m_table;

	qulonglong               m_lastId;
	std::size_t              m_batchSize;
	Replies                  m_replies;

public:

	/**
	 * Creates a new DatabaseDataProvider.
	 * The connection must be opened and the table must exist for the provider to work, otherwise
	 * an exception is thrown.
	 */
	DatabaseDataProvider(db::Connection::Ptr connection, db::TableDL::const_Ptr table);
	virtual ~DatabaseDataProvider();



	/**
	 * Returns the next reply (if available).
	 * If no reply is returned, then the DataProvider shall wait for
	 * several seconds, before trying again.
	 *
	 * This function is thread-safe.
	 */
	virtual Reply::Opt next();

	/**
	 * Inserts this reply into the DataProvider.
	 */
	virtual void insert(const Reply& reply);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
